<?php

class RedirectHelper {

	protected $parameters = array();
	protected $root;
	
	public function __construct($root = "main/index") {
		$this->root = $root;
	}
	
	public function go( $param ) {
		header("Location: " . BASE . "/" . $param);
	}
	
	public function setParameter($name,$value) {
		$this->parameters[$name] = $value;
		return $this;
	}
	
	public function getParameters() {
		$parms = "";
		if( count($this->parameters) > 0 )
			foreach( $this->parameters as $name => $value )
				$parms .= "/" . $name . "/" . $value;
		return $parms;
	}
	
	public function goToController($controller) {
		$this->go( $controller . "/index" . $this->getParameters() );
	}
	
	public function goToAction($action) {
		$this->go( $this->getCurrentController() . "/" . $action . $this->getParameters() );
	}
	
	public function goToControllerAction( $controller, $action ) {
		$this->go( $controller . "/" . $action . $this->getParameters() );
	}
	
	public function goToHome() {
		$this->go( ( count($this->parameters) != 0 ) ? $this->root . $this->getParameters() : "" );
	}
	
	public function goToUrl($url) {
		if( substr($url, 0, 4) == "http" ){
			header("Location: ".$url);
		}else{
			$pos = (strlen($url)-1);
			if( strrchr($url, '/') == $pos )
				$url = substr($url, 0, $pos);
			$this->go( $url . "/" . $this->getParameters() );
		}
	}
	
	public function getCurrentController() {
		global $start;
		return $start->getController();
	}
	
	public function getCurrentAction() {
		global $start;
		return $start->getAction();
	}
	
}

?>